#include "userdown.h"

UserDown::UserDown(datebase *db,QObject *parent) :
    QThread(parent)
{
    user_mydb=db;
}


void UserDown::run()
{
    qDebug()<<"=====下载固定车Start=====";
    down_user();
    qDebug()<<"=====下载固定车End=======";
}

void UserDown::down_user()
{
    QEventLoop loop;
    QNetworkAccessManager manager;
   // max_updated_at=user_mydb->select_updateTime(&pt_mydb->sqlite_db);
    QString max_updated_at="1970-01-01 01:00:00";
    QString str=downUserStr(max_updated_at);
    QUrl url(str);
    //qDebug()<<"down_car()max_updated_at:"<<max_updated_at<<str;
    QNetworkRequest request(url);
    QNetworkReply *reply=manager.get(request);
    connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
    loop.exec();
    Producer_WebUserMessage(reply);
}

bool UserDown::Producer_WebUserMessage(QNetworkReply *reply)
{
    QVariant statusCodeV =reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    QVariant redirectionTargetUrl =reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
    qDebug()<<"ProWebUserdown"<<statusCodeV.toString ()<<redirectionTargetUrl.toString ();
    if (reply->error() == QNetworkReply::NoError)
    {
        QTextCodec *codec = QTextCodec::codecForName("UTF-8");
        QString str = codec->toUnicode(reply->readAll());
        qDebug()<<"***strProWebCar***"<<str;
        if(str.length()<10)
        {
          //  emit MsgSignal(false);
            return false;
        }
        QScriptEngine engine;

        QScriptValue sc = engine.evaluate("value="+str);
        QString Ref=sc.property("ref").toString ();
        QString Mes=sc.property("msg").toString ();
        QScriptValueIterator it(sc.property("result"));
        while (it.hasNext())
        {
            QString Id,login,pass,userType;
            QString created_at,updated_at;
            user_info webUseInfo;

            it.next();
            if(!it.value().property("id").toString().isEmpty())
            {
                Id=it.value().property("id").toString();
                webUseInfo.id=Id;
            }
            if(!it.value().property("login").toString().isEmpty())
            {
                login=it.value().property("login").toString();
                webUseInfo.login=login;
            }
            if(!it.value().property("password").toString().isEmpty())
            {
                pass=it.value().property("password").toString();
                webUseInfo.password=pass;
            }
            if(!it.value().property("user_type").toString().isEmpty())
            {
                userType=it.value().property("user_type").toString();
                webUseInfo.user_type=userType;
            }

            if(!it.value().property("status").toString().isEmpty())
            {
                webUseInfo.status=it.value().property("status").toString();
             }

            if(!it.value().property("created_at").toString().isEmpty())
            {
                created_at=it.value().property("created_at").toString();
                webUseInfo.created_at=it.value().property("created_at").toString();

            }
            if(!it.value().property("updated_at").toString().isEmpty())
            {
                updated_at=it.value().property("updated_at").toString();
                webUseInfo.updated_at=it.value().property("updated_at").toString();
            }
            ////检查数据库固定车车辆数据
            if(webUseInfo.status=="正常")
            {
                user_mydb->insertUserData(&user_mydb->sqlite_db,&webUseInfo);
                qDebug()<<" writeSqliteCarweb";
                reply->deleteLater();
            }else {
                user_mydb->deleteUserData(&user_mydb->sqlite_db,webUseInfo.login);
                reply->deleteLater();
            }
        }
       // emit MsgSignal(true);
        return true;

    }else{
        //emit MsgSignal(false);
        return false;
    }
}

QString  UserDown::downUserStr(QString max_updated_at)
{
    QString str;
    str=("http://");
    str=str.append(sysInfo->serverIp).append("/api/v1/down_front_user");
    str.append("?controller_sn=").append(sysInfo->galleryNumber).append("&max_updated_at=").append(max_updated_at);
    qDebug()<<"str"<<str;
    return str;
}

